package com.lorenzogao.lib_view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.graphics.times
import com.lorenzogao.lib_view.ext.sp2px
import kotlin.math.abs


/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-14 11:04
 *  description:
 */

class LetterSideBar : View {


    private var mCurrentColor = Color.BLUE

    private var mTextColor = Color.BLACK

    private var mTextSize = 14


    val letter = arrayOf(
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z", "#"
    )


    private val mCurrentPaint by lazy {
        Paint().apply {
            color = mCurrentColor
            isAntiAlias = true
            textSize = mTextSize.sp2px(context).toFloat()
        }
    }

    private val mPaint by lazy {
        Paint().apply {
            color = mTextColor
            isAntiAlias = true
            textSize = mTextSize.sp2px(context).toFloat()
        }
    }
    private var mCurrentPosition = -1


     var listener: OnLetterSideBarListener? = null


    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val array = context?.obtainStyledAttributes(attrs, R.styleable.LetterSideBar)
        array?.apply {
            mCurrentColor = getColor(R.styleable.LetterSideBar_letterSideBarCurrentColor, mCurrentColor)
            mTextSize = getDimensionPixelSize(R.styleable.LetterSideBar_letterSideBarTextSize, mTextSize)
            mTextColor = getColor(R.styleable.LetterSideBar_letterSideBarTextSize, mTextColor)
        }

    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //Step 1 计算自定宽度
        var height = MeasureSpec.getSize(heightMeasureSpec)

//        val bounds = Rect()
//        mPaint.getTextBounds(letter[0], 0, letter[0].length, bounds)

        // 测量的是width
        val textWidth = mPaint.measureText("A")
        var width = paddingLeft + paddingRight + textWidth

        setMeasuredDimension(width.toInt(), height)

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN,
            MotionEvent.ACTION_MOVE -> {
                val moveY = event.y
                var textHeight = (height - paddingTop - paddingBottom) / letter.size
                var currentGrade = (moveY / (textHeight)).toInt()

                if (currentGrade<0){
                    currentGrade=0
                }
                if (currentGrade>letter.size-1){
                    currentGrade=letter.size-1
                }

                if (mCurrentPosition == currentGrade) {
                    return true
                }
                mCurrentPosition = currentGrade
                invalidate()
                listener?.onLetterrSelect(letter[mCurrentPosition])

            }
            MotionEvent.ACTION_UP -> {
                listener?.onLetterCancel()
            }
        }
        return true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val fontMetrics = mPaint.fontMetrics
        val dy = (abs(fontMetrics.ascent) + fontMetrics.descent) / 2 - fontMetrics.descent
        var bounds = Rect()
        mPaint.getTextBounds("A", 0, "A".length, bounds)
        var textHeight = (height - paddingTop - paddingBottom) / letter.size
        for ((index, value) in letter.withIndex()) {
            mPaint.getTextBounds(value, 0, value.length, bounds)
            val dx = width / 2 - bounds.width() / 2
            val letterCentenY = index * textHeight + textHeight / 2 + paddingTop
            val baseLine = dy + letterCentenY
            if (mCurrentPosition == index) {
                canvas.drawText(value, dx.toFloat(), baseLine, mCurrentPaint)
            } else {
                canvas.drawText(value, dx.toFloat(), baseLine, mPaint)
            }

        }
    }


    interface OnLetterSideBarListener {

        fun onLetterrSelect(value: String)

        fun onLetterCancel()


    }

}